import { ref, reactive, computed } from "vue";
import { defineStore } from "pinia";
import { pageHeros, heroDetail } from "@/api/hero";
import { pageEquipments } from "@/api/equipment";

export const useDialogStore = defineStore("dialog", () => {
  const dialogVisible = ref(false);
  const heroData = reactive({});
  const equipmentData = ref([]);

  const open = async (heroId) => {
    dialogVisible.value = true;
    // 根据英雄id请求英雄数据
    const res = await heroDetail(heroId);
    heroData.id = res.data.id;
    heroData.imgFileId = res.data.imgFileId;
    heroData.name = res.data.name;
    // 请求所有的装备数据
    const equipmentRes = await pageEquipments({ pageNum: 1,pageSize: 999999})
    equipmentData.value = equipmentRes.data.datas
  };
  const close = () => {
    dialogVisible.value = false;
  };

  const heroPage = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
    datas: [],
  });

  const pageDTO = reactive({
    pageNum: 1,
    pageSize: 10,
    name: "",
  });

  const getHeroPage = async () => {
    const res = await pageHeros(pageDTO);
    const data = res.data;
    heroPage.pageNum = parseInt(data.pageNum);
    heroPage.pageSize = parseInt(data.pageSize);
    heroPage.total = parseInt(data.total);
    heroPage.datas = data.datas;
    console.log(heroPage);
  };

  return {
    dialogVisible,
    heroData,
    heroPage,
    pageDTO,
    equipmentData,
    getHeroPage,
    open,
    close,
  };
});
